package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
)

/*
   Author: mahaiyuan
   Date: 2021/6/3 8:39 下午
   Description:
*/
func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:8888")
	if err != nil {
		panic(fmt.Sprintf("connect server error. %v", err))
	}
	defer conn.Close()

	reader := bufio.NewReader(os.Stdin)
	for true {
		data, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("read data error.", err)
			break
		}
		n, err := conn.Write([]byte(data)) //向服务器写数据
		if err != nil {
			fmt.Println("send data to server error.", err)
			break
		}
		fmt.Println("n = ", n)
		// 接收服务器响应内容
		rs := make([]byte, 1024)
		dataSize, err := conn.Read(rs)
		if err != nil {
			fmt.Println("read data from server error.", err)
			break
		}
		fmt.Println("收到服务器响应:", string(rs[:dataSize]))
	}
}